<template>
  <el-dialog
    :title="!dataForm.uuid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
         <el-form-item v-if="dataForm.uuid" label="订单号" prop="uuid">
          <el-tag >{{dataForm.uuid}}</el-tag>
    </el-form-item>
    <el-form-item label="发布用户" prop="userId">
     <el-input v-if="!dataForm.uuid" v-model="dataForm.userId" placeholder="发布订单的用户id"></el-input> 
        <el-tag v-else>{{dataForm.user.name}}</el-tag>
    </el-form-item>
    <el-form-item label="帮助人id" prop="helpId">
     <el-input v-if="!dataForm.uuid" v-model="dataForm.helpId" placeholder="帮助人id"></el-input>
          <el-tag v-if="dataForm.helpUser!=null">{{dataForm.helpUser.name}}</el-tag>
          <span v-else>无</span>
    </el-form-item>
    <el-form-item label="赏金" prop="money">
      <el-input v-model="dataForm.money" placeholder="赏金"></el-input>
    </el-form-item>
    <el-form-item label="帮助标题" prop="title">
      <el-input v-model="dataForm.title" placeholder="帮助标题"></el-input>
    </el-form-item>
    <el-form-item label="帮助详情id" prop="detailId">
      <el-input v-model="dataForm.detailId" placeholder="帮助详情id"></el-input>
    </el-form-item>
    <el-form-item label="帮助时间" prop="time">
     <el-input v-if="!dataForm.uuid" v-model="dataForm.time" placeholder="帮助时间"></el-input> 
         <el-tag v-else> {{dataForm.time}}</el-tag> 
    </el-form-item>
    <el-form-item label="目的地:" prop="addressId">
       <el-input v-if="!dataForm.uuid"   v-model="dataForm.addressId" placeholder="目的地:地址"></el-input>
       <el-tag v-else> {{dataForm.address.address+'-'+dataForm.address.detail}}</el-tag>
    </el-form-item>
    <el-form-item label="逻辑删除" prop="deleted">
     <!--  <el-input  v-model="dataForm.deleted" placeholder="逻辑删除"></el-input> -->
                   <el-switch
            v-model="dataForm.deleted"
            :active-value="0"
            :inactive-value="1"
            active-color="#13ce66"
            inactive-color="#ff4949"
          >
          </el-switch>
    </el-form-item>
    <el-form-item label="状态码" prop="status">
<!--       <el-input v-model="dataForm.status" placeholder="状态码"></el-input> -->
  <el-select v-model="dataForm.status" placeholder="请选择状态">
    <el-option
      v-for="item in statcode"
      :key="item.value"
      :label="item.name"
      :value="item.id">
    </el-option>
  </el-select>
    </el-form-item>
    <el-form-item label="选择学校" prop="schoolId">
  <!--     <el-input v-if="!dataForm.uuid" v-model="dataForm.schoolId" placeholder="学校的id"></el-input> -->
    <div>
    <el-cascader style="width:85%"
      filterabl
      clearable 
      placeholder="试试搜索:分类名"
      v-model="paths"
      :options="school"
      :props="setting"
    ></el-cascader>
  </div>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
       statcode:[
         {id:-1,
         name:"已取消"
         },
         {
          id:0,
          name:"需要帮助"
       },{
         id:1,
          name:"已帮助" 
       },{
         id:2,
          name:"帮助完成" 
       },{
         id:3,
          name:"等待确认" 
       },{
         id:4,
          name:"已完成" 
       }

       ],
         setting:{
        value: "id",
        label: "name",
        },
          paths:[],
        school:[],
        visible: false,
         dataForm: {
          uuid: 0,
          userId: '',
          helpId: '',
          money: '',
          title: '',
          detailId: '',
          time: '',
          addressId: '',
          deleted: '',
          status: '',
          schoolId: '',
          user:[],
          helpUser:[],
          address:[],

        },
        dataRule: {
          userId: [
            { required: true, message: '发布订单的用户id不能为空', trigger: 'blur' }
          ],
      
          money: [
            { required: true, message: '赏金不能为空', trigger: 'blur' }
          ],
          title: [
            { required: true, message: '帮助标题不能为空', trigger: 'blur' }
          ],
          detailId: [
            { required: true, message: '帮助详情id不能为空', trigger: 'blur' }
          ],
          time: [
            { required: true, message: '帮助时间不能为空', trigger: 'blur' }
          ],
          addressId: [
            { required: true, message: '目的地:地址不能为空', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '逻辑删除不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态码不能为空', trigger: 'blur' }
          ],
          schoolId: [
            { required: true, message: '学校的id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
          //获取学校
            getListSchool(){
                 this.$http({
              url: this.$http.adornUrl(`/help/school/list`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {  
                this.school=data.page.list; 
            })  
            },

      init (uuid) {
        this.getListSchool();
        this.dataForm.uuid = uuid || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.uuid) {
            this.$http({
              url: this.$http.adornUrl(`/help/orders/info/${uuid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.orderS.userId
                this.dataForm.helpId = data.orderS.helpId
                this.dataForm.money = data.orderS.money
                this.dataForm.title = data.orderS.title
                this.dataForm.detailId = data.orderS.detailId
                this.dataForm.time = data.orderS.time
                this.dataForm.addressId = data.orderS.addressId
                this.dataForm.deleted = data.orderS.deleted
                this.dataForm.status = data.orderS.status
                this.dataForm.schoolId = data.orderS.schoolId
                this.dataForm.school=data.orderS.school
                this.dataForm.user=data.orderS.user
                this.dataForm.helpUser=data.orderS.helpUser
                this.dataForm.address=data.orderS.address
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/help/orders/${!this.dataForm.uuid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'uuid': this.dataForm.uuid || undefined,
                'userId': this.dataForm.userId,
                'helpId': this.dataForm.helpId,
                'money': this.dataForm.money,
                'title': this.dataForm.title,
                'detailId': this.dataForm.detailId,
                'time': this.dataForm.time,
                'addressId': this.dataForm.addressId,
                'deleted': this.dataForm.deleted,
                'status': this.dataForm.status,
                'schoolId': this.paths[0]
              })
            }).then(({data}) => {
              this.paths=[]
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
